<template>
  <a-modal
    title="编辑房间租约"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="在租房间id"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入在租房间id" v-decorator="['roomId']" />
        </a-form-item>
        <a-form-item
          label="在租房间名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入在租房间名称" v-decorator="['roomName']" />
        </a-form-item>
        <a-form-item
          label="承租人姓名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入承租人姓名" v-decorator="['name', {rules: [{required: true, message: '请输入承租人姓名！'}]}]" />
        </a-form-item>
        <a-form-item
          label="聯係方式"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入聯係方式" v-decorator="['phone', {rules: [{required: true, message: '请输入聯係方式！'}]}]" />
        </a-form-item>
        <a-form-item
          label="身份证号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入身份证号" v-decorator="['idCard', {rules: [{required: true, message: '请输入身份证号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="身份证正面照"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入身份证正面照" v-decorator="['idCardImagesFront']" />
        </a-form-item>
        <a-form-item
          label="身份证反面照"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入身份证反面照" v-decorator="['idCardImagesBack']" />
        </a-form-item>
        <a-form-item
          label="相册 （图片id以逗号分隔开）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入相册 （图片id以逗号分隔开）" v-decorator="['images']" />
        </a-form-item>
        <a-form-item
          label="租期开始时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择租期开始时间" v-decorator="['rentStartDate',{rules: [{ required: true, message: '请选择租期开始时间！' }]}]" @change="onChangerentStartDate"/>
        </a-form-item>
        <a-form-item
          label="租期结束时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker style="width: 100%" placeholder="请选择租期结束时间" v-decorator="['rentEndDate',{rules: [{ required: true, message: '请选择租期结束时间！' }]}]" @change="onChangerentEndDate"/>
        </a-form-item>
        <a-form-item
          label="每月租金"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="每月押金"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        </a-form-item>
        <a-form-item
          label="付 （字典 付1-付18）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入付 （字典 付1-付18）" v-decorator="['receiptFu', {rules: [{required: true, message: '请输入付 （字典 付1-付18）！'}]}]" />
        </a-form-item>
        <a-form-item
          label="押 （字典 押1-押18-自定义19）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入押 （字典 押1-押18-自定义19）" v-decorator="['receiptYa', {rules: [{required: true, message: '请输入押 （字典 押1-押18-自定义19）！'}]}]" />
        </a-form-item>
        <a-form-item
          label="依据（字典 1日 2月 3年）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入依据（字典 1日 2月 3年）" v-decorator="['receiptYj', {rules: [{required: true, message: '请输入依据（字典 1日 2月 3年）！'}]}]" />
        </a-form-item>
        <a-form-item
          label=" 账单起始日（0 起租日 1日，2日......）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入 账单起始日（0 起租日 1日，2日......）" v-decorator="['billStart', {rules: [{required: true, message: '请输入 账单起始日（0 起租日 1日，2日......）！'}]}]" />
        </a-form-item>
        <a-form-item
          label="账单收租日日 （字典 0提前  1 固定）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入账单收租日日 （字典 0提前  1 固定）" v-decorator="['billEndType']" />
        </a-form-item>
        <a-form-item
          label="账单收租日提前几日"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入账单收租日提前几日" v-decorator="['billEndTqData']" />
        </a-form-item>
        <a-form-item
          label="账单收租日固定几月（有正负）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入账单收租日固定几月（有正负）" v-decorator="['billEndGdmonthData']" />
        </a-form-item>
        <a-form-item
          label="账单收租日固定几日"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入账单收租日固定几日" v-decorator="['billEndGddayData']" />
        </a-form-item>
        <a-form-item
          label="收租提醒开关（1 开启 0关闭）"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入收租提醒开关（1 开启 0关闭）" v-decorator="['rentReminderSwitch', {rules: [{required: true, message: '请输入收租提醒开关（1 开启 0关闭）！'}]}]" />
        </a-form-item>
        <a-form-item
          label="收租提醒提前 天"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入收租提醒提前 天" v-decorator="['rentReminderDay']" />
        </a-form-item>
        <a-form-item
          label="收租提醒提前 小时"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入收租提醒提前 小时" v-decorator="['rentReminderHour']" />
        </a-form-item>
        <a-form-item
          label="收租提醒提前 分钟"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入收租提醒提前 分钟" v-decorator="['rentReminderMinutes']" />
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入备注" v-decorator="['remark']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { houseLeaseEdit } from '@/api/modular/main/houselease/houseLeaseManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        rentStartDateDateString: '',
        rentEndDateDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              roomId: record.roomId,
              roomName: record.roomName,
              name: record.name,
              phone: record.phone,
              idCard: record.idCard,
              idCardImagesFront: record.idCardImagesFront,
              idCardImagesBack: record.idCardImagesBack,
              images: record.images,
              rent: record.rent,
              deposit: record.deposit,
              receiptFu: record.receiptFu,
              receiptYa: record.receiptYa,
              receiptYj: record.receiptYj,
              billStart: record.billStart,
              billEndType: record.billEndType,
              billEndTqData: record.billEndTqData,
              billEndGdmonthData: record.billEndGdmonthData,
              billEndGddayData: record.billEndGddayData,
              rentReminderSwitch: record.rentReminderSwitch,
              rentReminderDay: record.rentReminderDay,
              rentReminderHour: record.rentReminderHour,
              rentReminderMinutes: record.rentReminderMinutes,
              remark: record.remark
            }
          )
        }, 100)
        // 时间单独处理
        if (record.rentStartDate != null) {
            this.form.getFieldDecorator('rentStartDate', { initialValue: moment(record.rentStartDate, 'YYYY-MM-DD') })
        }
        this.rentStartDateDateString = moment(record.rentStartDate).format('YYYY-MM-DD')
        // 时间单独处理
        if (record.rentEndDate != null) {
            this.form.getFieldDecorator('rentEndDate', { initialValue: moment(record.rentEndDate, 'YYYY-MM-DD') })
        }
        this.rentEndDateDateString = moment(record.rentEndDate).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.rentStartDate = this.rentStartDateDateString
            values.rentEndDate = this.rentEndDateDateString
            houseLeaseEdit(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangerentStartDate(date, dateString) {
        this.rentStartDateDateString = dateString
      },
      onChangerentEndDate(date, dateString) {
        this.rentEndDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
